package com.example.sports_meet.fragment;


import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.sports_meet.AboutActivity;
import com.example.sports_meet.InformationActivity;
import com.example.sports_meet.R;
import com.example.sports_meet.SettingActivity;


public class ThirdFragment extends Fragment implements View.OnClickListener {

    TextView signup, clock_day;
    RelativeLayout imfor_btn, err_btn, setting_btn, about_btn;
    AlertDialog write;
    SharedPreferences sp;
    SharedPreferences.Editor editor;

    @SuppressLint("MissingInflatedId")
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_third, container, false);
        imfor_btn = view.findViewById(R.id.imfor_btn);
        about_btn = view.findViewById(R.id.about_btn);
        setting_btn = view.findViewById(R.id.setting_btn);
        signup = view.findViewById(R.id.signup);
        clock_day = view.findViewById(R.id.clock_day);


        imfor_btn.setOnClickListener(this);
        about_btn.setOnClickListener(this);
        setting_btn.setOnClickListener(this);
        signup.setOnClickListener(this);

        sp = requireActivity().getSharedPreferences("tice", Context.MODE_PRIVATE);
        clock_day.setText(sp.getString("tice", "0"));


        return view;
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.signup:
                setSignup();
                break;
            case R.id.imfor_btn:
                Intent intent3 = new Intent(getContext(), InformationActivity.class);
                startActivity(intent3);
                break;
            case R.id.about_btn:
                Intent intent4 = new Intent(getContext(), AboutActivity.class);
                startActivity(intent4);
                break;
            case R.id.setting_btn:
                Intent intent5 = new Intent(getContext(), SettingActivity.class);
                startActivity(intent5);
                break;


        }
    }

    public void setSignup() {

        write = new AlertDialog.Builder(getActivity()).create();
        View sigup_view = LayoutInflater.from(getActivity()).inflate(R.layout.pop_up_sign, null);
        write.setView(sigup_view);
        write.show();

        int count = Integer.parseInt(clock_day.getText().toString()) + 1;

        editor = sp.edit();
        editor.putString("tice", Integer.toString(count));
        editor.commit(); //写入
        onResume();//刷新


    }

    public void onResume() {
        super.onResume();

        SharedPreferences sp = getActivity().getSharedPreferences("tice", Context.MODE_PRIVATE);
        clock_day.setText(sp.getString("tice", "0"));
    }


}